<?php
/**
 * Created by PhpStorm.
 * User: kun
 * Date: 23/6/19
 * Time: 20:37
 */

namespace app\common\model;

use think\Db;
use think\Model;

class Comment extends Base
{
    /**
     * 通过条件获取评论的数量 v1
     * @param array $param
     * @return int|string
     */
    public function getNormalCommentsCountByCondition($param = [])
    {
        $count = Db::table('ent_comment')
            ->alias(['ent_comment' => 'a', 'ent_user' => 'b'])
            ->join('ent_user', 'a.status=1 AND a.user_id=b.id AND a.news_id=' . $param['news_id'])
            ->count();

        return $count;
    }

    /**
     *  获取评论列表 v1
     * @param array $param
     * @param int $from
     * @param int $size
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNormalCommentsByCondition($param = [], $from = 0, $size = 5)
    {
        $count = Db::table('ent_comment')
            ->alias(['ent_comment' => 'a', 'ent_user' => 'b'])
            ->join('ent_user', 'a.status=1 AND a.user_id=b.id AND a.news_id=' . $param['news_id'])
            ->limit($from, $size)
            ->order('a.id desc')
            ->select();

        return $count;

    }

    /**
     *获取评论总数 v2
     */
    public function getCountByCondition($param = [])
    {
        return $this->where($param)
            ->where('status', 1)
            ->field('id')
            ->count();

    }

    /**
     * 获取评论列表 v2
     * @param array $param
     * @param int $from
     * @param int $size
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getListsByCondition($param = [], $from = 0, $size = 5)
    {
        return $this->where($param)
            ->field('*')
            ->limit($from, $size)
            ->order('id desc')
            ->select();
    }


}